package com.kiss.rpc.channelhandler.handler;

import com.kiss.rpc.transport.message.MessageFormatConstant;
import io.netty.buffer.ByteBuf;

public class CommonFuncs {
    static void writeFullLength(ByteBuf byteBuf, byte[] body) {
        if (body != null) byteBuf.writeBytes(body);

        int bodyLength = body == null ? 0 : body.length;
        int writerIndex = byteBuf.writerIndex();
        byteBuf.writerIndex(MessageFormatConstant.MAGIC.length + MessageFormatConstant.VERSION_LENGTH +
                MessageFormatConstant.HEADER_FIELD_LENGTH);
        byteBuf.writeInt(MessageFormatConstant.HEADER_LENGTH + bodyLength);
        byteBuf.writerIndex(writerIndex);
    }
}
